﻿//---------------------------------------------------------------------------

#pragma hdrstop

#include "MotorComponent.h"
#include "math.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
TMotorComponent::TMotorComponent(TWinControl *Sender, int GB_num, int Min, int Max)
{
	ComponentDirStatus Dir = VERTICAL;
	int Top = 0;
	int Left = 0;

	this->Editable = false;
	this->Enabled = false;

	this->MGroup = new TGroupBox(Sender);
	this->MGroup->Parent = Sender;

	this->MShape = new TShape(this->MGroup);
	this->MShape->Parent = this->MGroup;

	this->MButtonMain = new TButton(this->MGroup);
	this->MButtonMain->Parent = this->MGroup;

	this->MEdit = new TEdit(this->MGroup);
	this->MEdit->Parent = this->MGroup;

	this->MBar = new TScrollBar(this->MGroup);
	this->MBar->Parent = this->MGroup;

	this->MButtonP = new TButton(this->MGroup);
	this->MButtonP->Parent = this->MGroup;
	this->MButtonN = new TButton(this->MGroup);
	this->MButtonN->Parent = this->MGroup;

	this->MCheck = new TCheckBox(this->MGroup);
	this->MCheck->Parent = this->MGroup;

	this->SetProperties(GB_num, Dir, Top, Left, Min, Max);
	this->SetEvents();
}
//---------------------------------------------------------------------------

TMotorComponent::TMotorComponent(TWinControl *Sender, int GB_num,
									ComponentDirStatus Dir, int Top, int Left, int Min, int Max)
{
	this->Editable = true;

	this->MGroup = new TGroupBox(Sender);
	this->MGroup->Parent = Sender;

	this->MShape = new TShape(this->MGroup);
	this->MShape->Parent = this->MGroup;

	this->MButtonMain = new TButton(this->MGroup);
	this->MButtonMain->Parent = this->MGroup;

	this->MEdit = new TEdit(this->MGroup);
	this->MEdit->Parent = this->MGroup;

	this->MBar = new TScrollBar(this->MGroup);
	this->MBar->Parent = this->MGroup;

	this->MButtonP = new TButton(this->MGroup);
	this->MButtonP->Parent = this->MGroup;
	this->MButtonN = new TButton(this->MGroup);
	this->MButtonN->Parent = this->MGroup;

	this->MCheck = new TCheckBox(this->MGroup);
	this->MCheck->Parent = this->MGroup;

	this->SetProperties(GB_num, Dir, Top, Left, Min, Max);
	this->SetEvents();

	this->Editable = false;
}
//---------------------------------------------------------------------------

TMotorComponent::~TMotorComponent()
{
	delete this->MGroup;
	delete this->MShape;
	delete this->MButtonMain;
	delete this->MEdit;
	delete this->MBar;
	delete this->MButtonP;
	delete this->MButtonN;
	delete this->MCheck;
}
//---------------------------------------------------------------------------

void TMotorComponent::SetProperties(int GB_num, ComponentDirStatus Dir, int Top, int Left, int Min, int Max)
{
	this->Name = "MC " + IntToStr(GB_num);
  this->MGroup->Name = AnsiString("MGroup") + IntToStr(GB_num);
	this->MGroup->Caption = this->Name;
	this->MGroup->Top = Top;
	this->MGroup->Left = Left;

	this->MShape->Name = AnsiString("MShape") + IntToStr(GB_num);
	this->MShape->Top = 12;
	this->MShape->Left = 6;
	this->MShape->Brush->Color = clGrayText;
	this->MShape->Brush->Style = bsSolid;

	this->MButtonMain->Name = AnsiString("MButtonMain") + IntToStr(GB_num);
	this->MButtonMain->Caption = "";

	this->MBar->Name = AnsiString("MBar") + IntToStr(GB_num);
	this->MBar->Min = Min;
	this->MBar->Max = Max;
	this->MBar->Position = ceil((float)(this->MBar->Max - this->MBar->Min) / 2);
//	this->MBar->Position = 0;

	this->MEdit->Name = AnsiString("MEdit") + IntToStr(GB_num);
	this->MEdit->Height = 25;
	this->MEdit->Width = 35;
	this->MEdit->Text = IntToStr(this->MBar->Position);

	this->MCheck->Name = AnsiString("MCheck") + IntToStr(GB_num);
	this->MCheck->Caption = "";
	this->MCheck->Width = 20;
	this->MCheck->Height = 20;

	this->MButtonP->Name = AnsiString("MButtonP") + IntToStr(GB_num);
	this->MButtonP->Width = 35;
	this->MButtonP->Height = 25;

	this->MButtonN->Name = AnsiString("MButtonN") + IntToStr(GB_num);
	this->MButtonN->Width = 35;
	this->MButtonN->Height = 25;

	this->SetDirection(Dir);
}
//---------------------------------------------------------------------------

void TMotorComponent::SetEvents()
{
	this->MButtonMain->OnMouseDown = MButtonMainMouseDown;
	this->MButtonMain->OnKeyDown = MButtonMainKeyDown;
  this->MButtonMain->OnKeyPress = MButtonMainKeyPress;

	this->MGroup->DragMode = dmManual;
	this->MGroup->OnDragOver = MGroupDragOver;
	this->MGroup->OnEndDrag = MGroupEndDrag;
	this->MGroup->OnMouseDown = MGroupMouseDown;
}
//---------------------------------------------------------------------------

void TMotorComponent::MoveMGroup(int Top, int Left)
{
	this->MGroup->Top = Top;
	this->MGroup->Left = Left;
}
//---------------------------------------------------------------------------

ComponentDirStatus TMotorComponent::GetDirection()
{
	return this->MDir;
}
//---------------------------------------------------------------------------

void TMotorComponent::SetDirection(ComponentDirStatus Dir)
{
	this->MDir = Dir;
	if(this->MDir == VERTICAL)
	{
		this->MGroup->Width = 110;
		this->MGroup->Height = 125;
		this->MShape->Width = 100;
		this->MShape->Height = 110;

		this->MButtonMain->Width = this->MShape->Width;
		this->MButtonMain->Height = 15;
		this->MButtonMain->Top = this->MShape->Top + this->MShape->Height - this->MButtonMain->Height;
		this->MButtonMain->Left = this->MShape->Left;

		this->MBar->Kind = sbVertical;
		this->MBar->Top = 20;
		this->MBar->Left = 52;
		this->MBar->Width = 25;
		this->MBar->Height = 80;

		this->MEdit->Top = 50;
		this->MEdit->Left = 10;

		this->MCheck->Top = 20;
		this->MCheck->Left = 85;

		this->MButtonP->Top = 80;
		this->MButtonP->Left = 10;
		this->MButtonP->Caption = "↓"; // ▼

		this->MButtonN->Top = 20;
		this->MButtonN->Left = 10;
		this->MButtonN->Caption = "↑"; // ▲
	}
	else //(this->MDir == HORIZONTAL)
	{
		this->MGroup->Width = 140;
		this->MGroup->Height = 90;
		this->MShape->Width = 128;
		this->MShape->Height = 75;

		this->MButtonMain->Width = this->MShape->Width;
		this->MButtonMain->Height = 15;
		this->MButtonMain->Top = this->MShape->Top + this->MShape->Height - this->MButtonMain->Height;
		this->MButtonMain->Left = this->MShape->Left;

		this->MBar->Kind = sbHorizontal;
		this->MBar->Top = 15;
		this->MBar->Left = 45;
		this->MBar->Width = 80;
		this->MBar->Height = 25;

		this->MEdit->Top = 45;
		this->MEdit->Left = 55;

		this->MCheck->Top = 20;
		this->MCheck->Left = 20;

		this->MButtonP->Top = 45;
		this->MButtonP->Left = 95;
		this->MButtonP->Caption = "→"; // ►

		this->MButtonN->Top = 45;
		this->MButtonN->Left = 12;
		this->MButtonN->Caption = "←"; // ◄
	}
}
//---------------------------------------------------------------------------

void TMotorComponent::SetEnabled(bool enabled)
{
	this->Enabled = enabled;
	this->MBar->Enabled = this->Enabled;
	this->MEdit->Enabled = this->Enabled;
	this->MButtonP->Enabled = this->Enabled;
	this->MButtonN->Enabled = this->Enabled;
  this->MCheck->Enabled = this->Enabled;
}
//---------------------------------------------------------------------------

bool TMotorComponent::GetEnabled()
{
	return this->Enabled;
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MGroupEndDrag(TObject *Sender, TObject *Target, int X,
          int Y)
{
	if((this->Editable) && (Target != NULL))
	{
		if(Target->InheritsFrom(__classid(TForm)))
//		if(Target == this->MGroup->Parent)
		{
			this->MGroup->Caption == "T";
			if((X != 0) && (Y != 0))
			{
				this->MGroup->Top = Y;
				this->MGroup->Left = X;
				this->MGroup->Caption = this->Name + ": (" + this->MGroup->Top
																+ ", " + this->MGroup->Left + ")";
			}
		}
		else if(Target == Sender)
		{
			if((X != 0) && (Y != 0))
			{
				this->MGroup->Top += Y;
				this->MGroup->Left += X;
				this->MGroup->Caption = this->Name + ": (" + this->MGroup->Top
																+ ", " + this->MGroup->Left + ")";
			}
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MGroupMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
	if(Button == mbLeft)
	{
		this->MGroup->BeginDrag(true);
	}
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MGroupDragOver(TObject *Sender, TObject *Source, int X,
          int Y, TDragState State, bool &Accept)
{
	if(Source->InheritsFrom(__classid(TGroupBox)))
		Accept = true;
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MButtonMainMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
					int X, int Y)
{
  if((this->Editable) && (Button == mbRight))
	{
		this->SetDirection(!this->MDir);
	}
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MButtonMainKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (Shift.Contains(ssCtrl)) {
		if((Key == 'E') || (Key == 'e'))
		{
			Editable = !Editable;
			if(Editable)
			{
				this->MGroup->Caption = this->Name + ": (" + this->MGroup->Top
															+ ", " + this->MGroup->Left + ")";
			}
			else
			{
				this->MGroup->Caption = this->Name;
      }
		}
		else if((Key == 'Q') || (Key == 'q'))
		{
			this->SetEnabled(!this->Enabled);
		}
		else if((Key == 'H') || (Key == 'h'))
		{
			this->MGroup->Top = -150;
			this->MGroup->Left = -150;
		}

		if(Editable)
		{
			if((Key == VK_UP) || (Key == 'W') || (Key == 'w'))
			{
				this->MGroup->Top--;
			}
			else if((Key == VK_DOWN) || (Key == 'S') || (Key == 's'))
			{
				this->MGroup->Top++;
			}
			else if((Key == VK_LEFT) || (Key == 'D') || (Key == 'd'))
			{
				this->MGroup->Left++;
			}
			else if((Key == VK_RIGHT) || (Key == 'A') || (Key == 'a'))
			{
				this->MGroup->Left--;
			}

			this->MGroup->Caption = this->Name + ": (" + this->MGroup->Top
															+ ", " + this->MGroup->Left + ")";
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TMotorComponent::MButtonMainKeyPress(TObject *Sender, wchar_t &Key)
{
	if(Editable)
	{
		if((Key == VK_UP) || (Key == 'W') || (Key == 'w'))
		{
			this->MGroup->Top -= 5;
		}
		else if((Key == VK_DOWN) || (Key == 'S') || (Key == 's'))
		{
			this->MGroup->Top += 5;
		}
		else if((Key == VK_LEFT) || (Key == 'D') || (Key == 'd'))
		{
			this->MGroup->Left += 5;
		}
		else if((Key == VK_RIGHT) || (Key == 'A') || (Key == 'a'))
		{
			this->MGroup->Left -= 5;
		}
		this->MGroup->Caption = this->Name + ": (" + this->MGroup->Top
														+ ", " + this->MGroup->Left + ")";
	}
}
//---------------------------------------------------------------------------
void TMotorComponent::SetPositionLimit(int min, int max)
{
//	int temp_position = this->MBar->Position;
	int temp_position = StrToInt(this->MEdit->Text);
	this->Min = min;
	this->Max = max;
	this->MBar->Min = min;
	this->MBar->Max = max;
	this->MBar->Position = temp_position;
}


